<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--card卡片-->
    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <el-table :data="rolesList" style="width: 100%" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item1,i1) in scope.row.children" :key="item1.id" :class="['bdbottom',i1===0?'bdtop':'']">
              <el-col :span="4">
                <!--一级权限-->
                <el-tag @close="delRight(item1.id,scope.row)" closable>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="20">
                <el-row class="rowClass" v-for="(item2,i2) in item1.children" :key="item2.id" :class="[i2===0?'':'bdtop']">
                  <el-col :span="5">
                    <!--二级权限-->
                    <el-tag @close="delRight(item2.id,scope.row)" closable type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="19">
                    <!--三级权限-->
                    <el-tag @close="delRight(item3.id,scope.row)" closable type="warning" v-for="item3 in item2.children" :key="item3.id">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="getRightsList(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!---->
    <!---->
    <!---->
    <!--树形控件分配权限-->
    <el-dialog title="分配权限" :visible.sync="dialogRightsTreeList" width="50%">
      <el-tree
        :default-checked-keys="defaultCheckedKeys"
        :data="rightsTreeList"
        show-checkbox
        default-expand-all
        node-key="id"
        ref="rightsTreeListRef"
        highlight-current
        :props="defaultProps">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogRightsTreeList = false">取 消</el-button>
        <el-button type="primary" @click="rightsTreeSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        rolesList: [],
        dialogRightsTreeList: false,
        rightsTreeList: [],
        defaultProps: {
          children: 'children',
          label: 'authName'
        },
        defaultCheckedKeys: [],
        roleId: 0
      }
    },
    created() {
      this.getRolesList()
    },
    methods: {
      // 获取所有角色列表
      getRolesList: async function () {
        const { data: res } = await this.$http.get('roles')
        if (res.meta.status !== 200) return this.$message.error('获取权限列表失败！')
        this.rolesList = res.data
      },
      // 1、获取角色所有权限列表，2、勾选已经获得权限
      getRightsList: async function (row) {
        // 1、获取角色所有权限列表
        const { data: res } = await this.$http.get('rights/tree')
        if (res.meta.status !== 200) return this.$message.error('获取角色权限列表失败！')
        this.rightsTreeList = res.data
        this.dialogRightsTreeList = true
        // 2、勾选已经获得权限
        this.roleId = row.id
        var rightId = []
        row.children.forEach(item => {
          item.children.forEach(item2 => {
            item2.children.forEach(item3 => {
              rightId.push(item3.id)
            })
          })
        })
        this.defaultCheckedKeys = rightId
      },
      // 更新角色授权
      rightsTreeSubmit: async function () {
        let rids = [
          ...this.$refs.rightsTreeListRef.getCheckedKeys(),
          ...this.$refs.rightsTreeListRef.getHalfCheckedKeys()
        ]
        rids = rids.join(',')
        console.log(rids)
        const { data: res } = await this.$http.post(`/roles/${this.roleId}/rights`, { rids: rids })
        if (res.meta.status !== 200) return this.$message.error('角色更新失败！')
        this.$message.success('角色更新成功！')
        this.getRolesList()
        this.dialogRightsTreeList = false
      },
      // 删除角色指定权限,rightId:权限id，roleId:角色id
      delRight: function (rightId, role) {
        this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
          if (res.meta.status !== 200) return this.$message.error('取消权限成功')
          role.children = res.data
          this.$message({
            type: 'success',
            message: '删除权限成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-tag{
    margin:10px;
  }
  .bdtop{
    border-top: 1px solid #eee;
  }
  .bdbottom{
    border-bottom: 1px solid #eee;
  }
  .el-row{
    display: flex;
    align-items: center;
  }
</style>
